# encoding:utf-8
import json,os,datetime,time
from flask import Flask, jsonify, request, render_template
from flask_cors import CORS
from libs.alterData import *

# configuration
app = Flask(__name__)
app.config.from_object(__name__)
DEBUG = True
app.jinja_env.auto_reload = True
app.config['TEMPLATES_AUTO_RELOAD'] = True
# enable CORS
CORS(app, resources={r'/*': {'origins': '*'}})


#告警数据查看
@app.route('/alter/alterLastDate', methods=['GET','POST'])
def alterlast():
    type = request.form.get("type")
    if(type == 'severity'):
        response_object=getData().alterInfoDate()
    else:
        response_object = getData().alterLastDate()
    return  render_template('alter.html',alterlist=response_object)

#过滤表
@app.route('/alter/filter', methods=['GET'])
def test():
    response_object=getData().getFilterTable()
    return  render_template('filter.html',FilterDate=response_object)

#更新或新增过滤规则
@app.route('/alter/filteraddDate', methods=['POST'])
def addDate():
    id = request.form.get("id")
    id = int(id)
    filterName = request.form.get("filterName")
    filterHost = request.form.get("filterHost")
    filterRedmin = request.form.get("filterRedmin")
    if (id == 0):
        changeDate().addFilterTable(filterName, filterHost, filterRedmin)
    else:
        changeDate().editFilterTable(str(id), filterName, filterHost, filterRedmin)
    response_object = getData().getFilterTable()
    return  render_template('filter.html',FilterDate=response_object)

#删除过滤规则
@app.route('/alter/filterdeleteDate/<id>', methods=['GET'])
def deleteDate(id):
    id = int(id)
    changeDate().deleteFilterTable(id)
    response_object = getData().getFilterTable()
    return  render_template('filter.html',FilterDate=response_object)

#静默数据查收
@app.route('/alter/getReply/<silenceTime>', methods=['GET'])
def getReply(silenceTime):
    dtime = datetime.datetime.now()
    un_time = time.mktime(dtime.timetuple())
    os.system('echo {},{} >>/usr/local/shell/alter/readfile/alterStatus.csv'.format(un_time,silenceTime))
    return jsonify(silenceTime)

# #按主题分类的告警查看
# @app.route('/alter/alterInfoDate', methods=['GET'])
# def alterInfo():
#     response_object=getData().alterInfoDate()
#     return


if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port="20001")


#按时间排序,按告警等级+类型排序,按主机业务排序

#过滤 快速